package com.qingmeng.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmeng.apicommon.model.entity.InterfaceInfo;
import com.qingmeng.apicommon.model.entity.User;
import com.qingmeng.apicommon.model.entity.UserInterfaceInfo;
import com.qingmeng.apicommon.service.RemoteService;
import com.qingmeng.apicommon.common.ErrorCode;
import com.qingmeng.apicommon.exception.BusinessException;
import com.qingmeng.project.mapper.InterfaceInfoMapper;
import com.qingmeng.project.mapper.UserInterfaceInfoMapper;
import com.qingmeng.project.mapper.UserMapper;
import com.qingmeng.project.service.UserInterfaceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class RemoteServiceImpl implements RemoteService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;
    @Resource
    private UserInterfaceInfoMapper userinterfaceinfomapper;

    /**
     * 根据accesskey，获取用户信息，判断用户是否存在，存在返回用户信息
     * @param accessKey
     * @return
     */
    @Override
    public User getInvokerUser(String accessKey) {
        if (StringUtils.isAnyBlank(accessKey)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accessKey", accessKey);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据接口路径，和请求方式，判断模拟接口是否存在，存在返回接口详细信息
     * @param path
     * @param method
     * @return
     */
    @Override
    public InterfaceInfo isInvokeMock(String path, String method) {
        if (StringUtils.isAnyBlank(path,method)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", path)
                .eq("request_type", method);
        return interfaceInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 调用接口统计，接口调用成功，用户调用接口次数加1
     * @param userId
     * @param interfaceId
     */
    @Override
    public boolean invokeCountNum(long userId, long interfaceId) {
        if (userId <= 0 || interfaceId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userInterfaceInfoService.invokeCountNum(userId, interfaceId);
    }

    /**
     * 查询用户剩余调用次数
     * @param userId
     * @param interfaceId
     * @return
     */
    @Override
    public boolean invokeHaveNum(long userId, long interfaceId) {
        if ((userId <= 0 || interfaceId <= 0)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("interface_id", interfaceId);
        UserInterfaceInfo userInterfaceInfo = userinterfaceinfomapper.selectOne(queryWrapper);
        if (userInterfaceInfo.getHaveNum() <= 0){
            throw new BusinessException(ErrorCode.REQUEST_ERROR);
        }
        return true;
    }
}
